<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <!--  限制输入必须为number  -->
        <input type="text" v-model.number="seach">
        <button @click="order=1">升序</button>
        <button @click="order=-1">降序</button>
        <ul>
            <li v-for="(item, index) in newPerson" :key="item.id">
                {{item.name}}----{{item.age}}
            </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        [].filter
        const vm = new Vue({
            el: "#app",
            data() {
                return {
                    person: [
                        { id: "001", name: "lucy", age: 22 },
                        { id: "002", name: "lili", age: 12 },
                        { id: "003", name: "yuehan", age: 31 },
                        { id: "004", name: "liming", age: 18 },
                        { id: "005", name: "xiaowang", age: 4 },
                        { id: "006", name: "zhangsan", age: 19 },
                        { id: "007", name: "wangwu", age: 40 },
                    ],
                    // 由于原数据不可能更改,必须将渲染数据绑定到另外一个渲染数据中
                    newPerson: [],
                    seach: 0,
                    order: 0
                };
            },
            // computed: {
            //     newPerson() {
            //         console.log(this.seach);
            //         return this.person.filter(item => {
            //             return item.age > this.seach
            //         })
            //     }
            // },
            watch: {
                seach: {
                    //  添加初始侦听,及在页面还未加载数据时就开始侦听,当第一次加载数据时就由空变为了数据,视为一次更改
                    immediate: true,
                    // 使用计算属性的值也可以使用侦听处理
                    handler(newVal) {
                        this.newPerson = this.person.filter(item => {
                            return item.age > newVal
                        })
                    }
                },
                order: {
                    handler(newVal) {
                        this.newPerson.sort((a, b) => {
                            return a.age - b.age
                        })
                    }
                }

            }
        });
    </script>
</body>

</html>